﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Yz.Base;
using Yz.Core.Base;

namespace Yz.Cache.Redis
{
    public class RedisMq : IDependency, IRedisMq
    {
        #region 同步
        /// <summary>
        /// 发布消息（model）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="paraCacheMq"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public long Publish<T>(ParaCacheMq paraCacheMq, T model)
        {
            CacheRedisProvider redisProvider = new CacheRedisProvider(paraCacheMq.DbNum);
            return redisProvider.Publish(paraCacheMq.Channel, model);
        }
        /// <summary>
        /// 发布消息（list）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="paraCacheMq"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public long Publish<T>(ParaCacheMq paraCacheMq, List<T> list)
        {
            CacheRedisProvider redisProvider = new CacheRedisProvider(paraCacheMq.DbNum);
            return redisProvider.Publish(paraCacheMq.Channel, list);
        }
        /// <summary>
        /// 订阅
        /// </summary>
        /// <param name="paraCacheMq"></param>
        /// <param name="handler"></param>
        public void Subscribe(ParaCacheMq paraCacheMq, Delegate callBack)
        {
            CacheRedisProvider redisProvider = new CacheRedisProvider(paraCacheMq.DbNum);
            redisProvider.Subscribe(paraCacheMq.Channel, callBack);
        }
        /// <summary>
        ///     Redis发布订阅  取消订阅
        /// </summary>
        /// <param name="channel"></param>
        public void Unsubscribe(ParaCacheMq paraCacheMq)
        {
            CacheRedisProvider redisProvider = new CacheRedisProvider(paraCacheMq.DbNum);
            redisProvider.Unsubscribe(paraCacheMq.Channel);
        }

        /// <summary>
        ///     Redis发布订阅  取消全部订阅
        /// </summary>
        public void UnsubscribeAll(int dbNum)
        {
            CacheRedisProvider redisProvider = new CacheRedisProvider(dbNum);
            redisProvider.UnsubscribeAll();
        }
        #endregion

        #region  异步
        /// <summary>
        /// 发布消息（model）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="paraCacheMq"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<long> PublishAsync<T>(ParaCacheMq paraCacheMq, T model)
        {
            CacheRedisProvider redisProvider = new CacheRedisProvider(paraCacheMq.DbNum);
            return await redisProvider.PublishAsync(paraCacheMq.Channel, model);
        }
        /// <summary>
        /// 发布消息（list）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="paraCacheMq"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public async Task<long> PublishAsync<T>(ParaCacheMq paraCacheMq, List<T> list)
        {
            CacheRedisProvider redisProvider = new CacheRedisProvider(paraCacheMq.DbNum);
            return await redisProvider.PublishAsync(paraCacheMq.Channel, list);
        }
        /// <summary>
        /// 订阅
        /// </summary>
        /// <param name="paraCacheMq"></param>
        /// <param name="handler"></param>
        public async Task SubscribeAsync(ParaCacheMq paraCacheMq, Delegate callBack)
        {
            CacheRedisProvider redisProvider = new CacheRedisProvider(paraCacheMq.DbNum);
            await redisProvider.SubscribeAsync(paraCacheMq.Channel, callBack);
        }
        /// <summary>
        ///     Redis发布订阅  取消订阅
        /// </summary>
        /// <param name="channel"></param>
        public async Task UnsubscribeAsync(ParaCacheMq paraCacheMq)
        {
            CacheRedisProvider redisProvider = new CacheRedisProvider(paraCacheMq.DbNum);
            await redisProvider.UnsubscribeAsync(paraCacheMq.Channel);
        }
        /// <summary>
        ///     Redis发布订阅  取消全部订阅
        /// </summary>
        public async Task UnsubscribeAllAsync(int dbNum)
        {
            CacheRedisProvider redisProvider = new CacheRedisProvider(dbNum);
            await redisProvider.UnsubscribeAllAsync();
        }
        #endregion
    }
}
